package com.amado.course50java8;

import java.util.Arrays;
import java.util.Comparator;
import java.util.function.Consumer;

/**
 * 方法
 * 
 * @author Administrator
 *
 */
public class Java804MethodRefernce {
	public static void main(String[] args) {
		foreach();
		thread();
		sort();
	}

	private static void foreach() {
		// java 8 匿名类
		Arrays.asList("a", "b", "d").forEach(new Consumer<String>() {
			@Override
			public void accept(String t) {
				System.out.print(t);
			}
		});
		System.out.println();
		// java 8 lambda,前提条件必须为@FunctionalInterface（函数接口）申明的类
		Arrays.asList("a", "b", "d").forEach(e -> System.out.print(e));
		// java 8 方法引用
		Arrays.asList("a", "b", "d").forEach(System.out::print);
		// java 8 方法引用
		Consumer<String> consumer  = (e)->System.out.print(e);
		Arrays.asList("a", "b", "d").forEach(consumer::accept);

	}

	public static void thread() {
		// java 8 匿名类
		new Thread(new Runnable() {
			@Override
			public void run() {
				System.out.println("come");
			}
		}).start();
		// java 8 run方法没有参数
		new Thread(() -> System.out.println("come")).start();
		//自动匹配方法（参数个数）
		new Thread(System.out::println).start();
		Runnable runnable=()->System.out.println("come");
		new Thread(runnable).start();
		
	}

	public static void sort() {
		// java 匿名类
		Arrays.sort(new String[] { "a", "c", "b" }, new Comparator<String>() {
			@Override
			public int compare(String o1, String o2) {
				return o1.compareTo(o2);
			}
		});
		// 如果抽象方法有多个参数，多个参数用（）括起来,，int compare(T o1, T o2);有个参数,缺省值是不需要return的
		// e1,e2是参数
		Arrays.sort(new String[] { "a", "c", "b" }, (e1, e2) -> e1.compareTo(e2));
		// java 8 method refernce 相当于e1.compareTo(e2)
		//匹配e1.compareTo(e2),static compareTo(e1,e2)
		Arrays.sort(new String[] { "a", "c", "b" }, String::compareTo);
		// java 8 method refernce
		Arrays.sort(new String[] { "a", "c", "b" }, Java804MethodRefernce::compare);
		// 总结
//		(e1, e2) -> e1.compareTo(e2) 等价于 String::compareTo 等价于 Java802MethodRefernce::compare
	}

	public static int compare(String a, String b) {
		return a.compareTo(b);
	}
}
